package com.moon.controller;

import com.moon.annotations.LogOperate;
import com.moon.pojo.*;
import com.moon.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

//    @GetMapping
//    public Result getEmps(@RequestParam(defaultValue = "1") Integer page,
//                          @RequestParam(defaultValue = "10") Integer pageSize){
//        log.info("分页查询，参数：page={},pageSize={}",page,pageSize);
//        PageResult<Emp> pageResult =  empService.page(page, pageSize);
//        return Result.success(pageResult);
//    }


    @GetMapping
    public Result getEmps(EmpQueryParam empQueryParam, LoginInfo loginInfo){
        log.info("当前登录用户如下{}", loginInfo);
        log.info("分页查询，参数：empQueryParam={}",empQueryParam);
        PageResult<Emp> pageResult =  empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    @LogOperate
    public Result addEmp(@RequestBody Emp emp){
        log.info("添加员工，参数：emp={}",emp);
        empService.addEmp(emp);
        return Result.success();
    }

    @DeleteMapping
    @LogOperate
    public Result deleteEmpByIds(@RequestParam List<Integer> ids){
        log.info("删除员工，参数：id={}",ids);
        empService.deleteEmpByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id){
        log.info("查询员工，参数：id={}",id);
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }

    @PutMapping
    @LogOperate
    public Result updateEmp(@RequestBody Emp emp){
        log.info("修改员工，参数：emp={}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }
}
